In this lecture will selectively process
game objects in our game using a list and a four loop. And the title may have already given
this away, the game objects that we're going to process our teddy bears and
by process, I mean blow up. Let's go see how we can do that. As usual I'll start by showing
you how the game works, so when I run the game I'll be spawning
teddy bears of three different colors. And for the green teddy bears I can press
the right mouse button and destroy it. For yellow teddy bears,
I can press the left mouse button and for purple teddy bears I can press
the middle mouse button. And I can keep blowing up teddy bears for
as long as I want. Our main camera still has the teddy
bear spawner script attached to it, but I've also attached a blowing
up teddy's script. So this is the game manager
that will manage the different game objects in the scene. And I've added a Prefab explosion and
sprites for the three different teddy bears. And this reminds you that we can have
multiple scripts attached to a single game object in our scene. Let's go take a look at the two new
scripts that I've added to our game. The teddy color script is
not particularly exciting, it's just an enumeration of the three
different teddy bear colors. So I have green, purple and yellow teddy
bears in my game, and you'll see where I use this Enumeration to specify the color
of the teddy bears that I want to blow up. All the heavy lifting for this game
is in the blowing up teddy script. We already saw the serialized fields that
I have I also have a list of game objects. I'm regularly going to retrieve a list
of game objects and rather than creating a new list every time I do that,
I've just declared a field here so I can just reuse this field every time I need
to retrieve that list of game objects. In the update method, I'm going to try to find all the objects
in the game that are game objects. We know from the find objects
of type method documentation in the scripting reference though,
that that method is slow. So we only want to call that method
if we actually have input on one of the input axes that we use
to blow up teddy bears. I've defined three different input axes,
one for blowing up yellow teddy's, one for blowing up green teddies and
one for blowing up purple teddy's. So I'm only going into the body
of this if statement if I actually have input on at
least one of those input axes. If I do, I clear my list of game
objects because my list of game objects is probably stale at this point,
I probably spawned new teddy bears and I may have destroyed some teddy bears. So I don't want to use an out of date
list of the game objects in my game. I want to get a current list of
game objects in the game, so I clear my game objects list. And then I call that method that we
know from the documentation is slow, and I find all the game objects that
are currently in the scene and that method returns an array,
but I'm using a list. So I'm going to now call
the ad range method for the list to add each element
of that array into my list. So this method is different from the ad
method that we've seen in the past, where we would add single
elements one at a time. The ad range method actually
takes in an array or a number of other data types like
another list and will copy those elements from the argument that we
provide to the method into this list. So at this point we have a list of
all the game objects in our game. Now we have a set of if statements that
check if in fact we're trying to blow up yellow teddy bears were going
to call a different method. And we pass in the color of the teddy
bears were trying to blow up in that list of game objects that
we just retrieved above. I do the same thing for the blow up
green teddies and blow up purple teddy's axes of course passing in a different
color that I'm trying to blow up. You'll notice that I'm using if statements
here, not else if's or else's because I want it to be possible for the player
to actually press multiple mouse buttons. Or provide input on multiple
of these inputs axes and blow up teddy bears of multiple colors,
all on the same frame. The blow up teddy's method is going to
blow up the teddy bears of the color that's provided here. You'll see that I'm using a backwards for
loop, although I'm not actually removing any items from
the list as I do this processing. I do know that I'm going to actually
be destroying those game objects. So even though I'm not
removing them from the list, I'm conceptually removing them from
the set of game objects in the game. So using a backwards four loop is
a safe thing to do at this point. The next thing I do is for
the current element in the list, I get the sprite render our component and
I put it into this variable. I have to check to see if the sprite
render is not null because one of the game objects that always gets
returned is the main camera. And the main camera isn't a teddy
bear we're trying to blow up and the main camera doesn't have a sprite
render component attached to it. So if I tried to process the main camera, getting the sprite renderers component
would put null into this variable. And then when I came down here and tried
to access the sprite property of that null object, we get a null reference
exception thrown and that's a bad thing. So we want to avoid that happening and
we can do that by using this if statement. Now I have a complicated bullying
expression that checks if the color were trying to destroy his green, and the sprite attached to this game object
is actually the green teddy bear sprite. That means we just found
a green teddy bear and we're trying to destroy green teddy bears. Or if we're trying to destroy
purple teddy bears and this current game object has the purple
teddy bear sprite or were trying to destroy yellow teddy bears and this game
object has the yellow teddy bear sprite. Those are also possibilities for finding a teddy bear that we
should currently blow up. So if any of these three things are true, then we know we found a teddy bear of
the color that we're trying to blow up. So we called the blow up teddy method with
the current teddy bear as the argument. The blow up teddy bear method is
pretty simple, all I do is I inst and she ate the explosion prefab and
I destroy that teddy bear. So that's the heavy lifting for
making it so we can destroy teddy bears of different
colors based on player input to recap. In this lecture, you learned how we can
selectively process game objects in our game using a list and a four loop. And you also learned that if we're going
to remove elements from the list as we're navigating or iterating over
that list, we can't use a for each loop, and
we also can't use a forward four loop. We need to use a backwards four a loop
to make sure we don't skip over and of the elements that should
be removed from the list.